package com.example.biji.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.biji.dao.NoteDao;
import com.example.biji.model.Note;
import com.example.biji.R;

public class AddNoteActivity extends AppCompatActivity {
    private EditText etTitle, etContent;
    private Button btnSave;
    private NoteDao noteDao;
    private int userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_note);

        userId = getIntent().getIntExtra("userId", -1);
        if (userId == -1) {
            Toast.makeText(this, "用户ID获取失败", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }

        initViews();
        setupListeners();
    }

    private void initViews() {
        etTitle = findViewById(R.id.et_add_title);
        etContent = findViewById(R.id.et_add_content);
        btnSave = findViewById(R.id.btn_add_save);
        noteDao = new NoteDao(this);
    }


    private void setupListeners() {
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override


            public void onClick(View v) {
                saveNote();
            }
        });


    }

    private void saveNote() {
        String title = etTitle.getText().toString().trim();
        String content = etContent.getText().toString().trim();

        if (title.isEmpty()) {
            Toast.makeText(this, "标题不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        // 使用带参数的构造函数创建Note对象
        Note note = new Note(title, content, userId);
        long result = noteDao.insertNote(note);

        if (result > 0) {
            Toast.makeText(this, "笔记添加成功", Toast.LENGTH_SHORT).show();
            finish();
        } else {
            Toast.makeText(this, "添加失败，请重试", Toast.LENGTH_SHORT).show();
        }
    }
}